package org.dromara.exam.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.exam.domain.vo.ExamWordBookVo;
import org.dromara.exam.domain.bo.ExamWordBookBo;
import org.dromara.exam.service.IExamWordBookService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 单词本主表
 *
 * @author Lion Li
 * @date 2025-09-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/exam/wordBook")
public class ExamWordBookController extends BaseController {

    private final IExamWordBookService examWordBookService;

    /**
     * 查询单词本主表列表
     */
    @SaCheckPermission("exam:wordBook:list")
    @GetMapping("/list")
    public TableDataInfo<ExamWordBookVo> list(ExamWordBookBo bo, PageQuery pageQuery) {
        return examWordBookService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出单词本主表列表
     */
    @SaCheckPermission("exam:wordBook:export")
    @Log(title = "单词本主表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ExamWordBookBo bo, HttpServletResponse response) {
        List<ExamWordBookVo> list = examWordBookService.queryList(bo);
        ExcelUtil.exportExcel(list, "单词本主表", ExamWordBookVo.class, response);
    }

    /**
     * 获取单词本主表详细信息
     *
     * @param bookId 主键
     */
    @SaCheckPermission("exam:wordBook:query")
    @GetMapping("/{bookId}")
    public R<ExamWordBookVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long bookId) {
        return R.ok(examWordBookService.queryById(bookId));
    }

    /**
     * 新增单词本主表
     */
    @SaCheckPermission("exam:wordBook:add")
    @Log(title = "单词本主表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ExamWordBookBo bo) {
        return toAjax(examWordBookService.insertByBo(bo));
    }

    /**
     * 修改单词本主表
     */
    @SaCheckPermission("exam:wordBook:edit")
    @Log(title = "单词本主表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ExamWordBookBo bo) {
        return toAjax(examWordBookService.updateByBo(bo));
    }

    /**
     * 删除单词本主表
     *
     * @param bookIds 主键串
     */
    @SaCheckPermission("exam:wordBook:remove")
    @Log(title = "单词本主表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{bookIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] bookIds) {
        return toAjax(examWordBookService.deleteWithValidByIds(List.of(bookIds), true));
    }
}
